package org.yawnage.framework.server 
{
	import flash.events.Event;
	import mx.events.FlexEvent;
	import org.yawnage.framework.Broker;
	import spark.components.View;
	 /**
	  * <p>//TODO:To be written</p>
	  * @author Berry Jordaan
	  * @date 2011/11/11 07:05 PM
	  * @copyright  Copyright © Berry/James 2011
	  * @version 1.0.0.0
	  * @description
	  * @history Berry Jordaan on 2011/11/11 07:05 PM: Created
	  */
	public class RegisterView extends Publisher
	{
		static public const VIEW_READY:String = "viewReady";
		static public const VIEW_EXPIRE:String = "viewExpire";

		public function RegisterView(viewName:String, view:View) 
		{
			super(viewName);
			setClassDescription("org.yawnage.framework.server.RegisterView");
			
			Broker.instance.register(this);
			
			view.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
			view.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
		}
		
		override public function defineServices():void
		{
			registerService(this, VIEW_READY);
			registerService(this, VIEW_EXPIRE);
		}
		
		private function onRemoved(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			syndicate(this, VIEW_EXPIRE);
			unRegisterService(VIEW_READY);
			unRegisterService(VIEW_EXPIRE);
			Broker.instance.unRegisterByName(this.name);
		}
		
		private function onCreationComplete(e:FlexEvent):void 
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
			syndicate(this, VIEW_READY, View(e.target));
		}
		
	}

}